package com.dhcc.android.base.utils;

import android.app.Activity;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.dhcc.android.base.R;

import java.util.List;
import java.util.Map;

/**
 * 混合标签页UI工具类
 *
 * @author zhoudan
 * @date 2016年4月8日
 */
public class MixUIUtil {

    public static final String pfmKey = "MJPFM";

    private Activity activity;
    private List<Map<String, String>> datas;
    private OnMixListOperateListener listener;
    private MixUIListener mMixUIListener;

    private String key;

    /**
     * 1--地物    2--设施农业用地
     */
    private int type;

    private boolean delFlag = false;
    /**
     * 单位标记    1--亩   2--平方米
     */
    private int unitFlag = 1;

    private String mjkey;

    public MixUIUtil(Activity activity, List<Map<String, String>> datas, String key, int type, MixUIListener mixUIListener) {
        this.activity = activity;
        this.datas = datas;
        this.key = key;
        this.type = type;
        this.mMixUIListener = mixUIListener;

        if (type == 1) {
            mjkey = "ZWMJ";
        } else {
            mjkey = "SNZWMJ";
        }
    }

    public void setUnitFlag(int unitFlag) {
        this.unitFlag = unitFlag;
    }

    public void setDelFlag(boolean delFlag) {
        this.delFlag = delFlag;
    }

    public void setOnMixListOperateListener(OnMixListOperateListener listener) {
        this.listener = listener;
    }


    public void refresh(LinearLayout listContent, List<Map<String, String>> datas) {
        this.datas = datas;
        initListView(listContent);
        listContent.postInvalidate();
    }

    public void initListView(LinearLayout listContent) {
        listContent.removeAllViews();

        for (int i = 0; i < datas.size(); i++) {
            final int position = i;
            View row = LayoutInflater.from(activity).inflate(R.layout.util_mix_list_item_fixed, null);

            Map<String, String> map = datas.get(i);

            final CheckBox checkbox = (CheckBox) row.findViewById(R.id.checkbox);
            LinearLayout checkboxlayout = (LinearLayout) row.findViewById(R.id.checkbox_layout);
            LinearLayout subcontent = (LinearLayout) row.findViewById(R.id.sub_content);

            LinearLayout valueLayout = (LinearLayout) row.findViewById(R.id.value_layout);
            TextView value = (TextView) row.findViewById(R.id.value);
            TextView mj_label = (TextView) row.findViewById(R.id.mj_prop_label);
            final EditText mj_edit = (EditText) row.findViewById(R.id.mj_edit_prop);

            //几何计算
            final Button jhjs_btn = (Button) row.findViewById(R.id.jhjs_btn);

            LinearLayout fz_prop = (LinearLayout) row.findViewById(R.id.fz_prop_layout);
            EditText fz_edit = (EditText) row.findViewById(R.id.fz_edit_prop);

            value.setText(map.get(key));

            //判断是否开启删除操作
            if (delFlag) {
                checkboxlayout.setVisibility(View.VISIBLE);
                subcontent.setVisibility(View.GONE);
            } else {
                checkboxlayout.setVisibility(View.GONE);
                subcontent.setVisibility(View.VISIBLE);
            }

            //判断图斑类型
            if (type == 1) {
                String mj = StringUtil.emptyValueFormatter(map.get(mjkey));
                if (unitFlag == 1) {
                    mj_label.setText("面积(亩)");
                } else {
                    mj_label.setText("面积(平方米)");
                    mj = StringUtil.emptyValueFormatter(map.get(pfmKey));
                }

                mj_edit.setText(mj);

                mj_edit.addTextChangedListener(new TextChange(mj_edit, mjkey, i));
            } else if (type == 2) {
                fz_prop.setVisibility(View.VISIBLE);

                String mj = StringUtil.emptyValueFormatter(map.get(mjkey));
                if (unitFlag == 1) {
                    mj_label.setText("面积(亩)");
                } else {
                    mj_label.setText("面积(平方米)");
                    mj = StringUtil.emptyValueFormatter(map.get(pfmKey));
                }
                mj_edit.setText(mj);


                String fz = StringUtil.emptyValueFormatter(map.get("SNFZCS"));
                fz_edit.setText(fz);

                mj_edit.addTextChangedListener(new TextChange(mj_edit, mjkey, i));
                fz_edit.addTextChangedListener(new TextChange(fz_edit, "SNFZCS", i));
            }
            jhjs_btn.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    if (listener != null) {
                        listener.OnBtnClick(position, jhjs_btn, mj_edit);
                    }
                }
            });

            checkbox.setOnCheckedChangeListener(null);
            if ("1".equals(map.get("checkFlag"))) {
                checkbox.setChecked(true);
            } else {
                checkbox.setChecked(false);
            }

            if (delFlag) {
                valueLayout.setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        boolean ck = checkbox.isChecked();
                        checkbox.setChecked(!ck);
                    }
                });
            }

            checkbox.setOnCheckedChangeListener(new OnCheckedChangeListener() {

                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    if (listener != null) {
                        listener.OnCheckClick(position, isChecked);
                    }
                }
            });


            listContent.addView(row);
        }
    }


    public class TextChange implements TextWatcher {
        private TextView txv;
        private String key;
        private int position;

        public TextChange(TextView txv, String key, int position) {
            this.txv = txv;
            this.key = key;
            this.position = position;
        }

        @Override
        public void afterTextChanged(Editable s) {
            String value = txv.getText().toString().trim();
            Map<String, String> map = datas.get(position);
            if (key.contains("MJ")) {
                if (unitFlag == 1) {
                    map.put(key, value);
                    map.put(pfmKey, CommenUtil.MUtoPFM(value));
                } else if (unitFlag == 2) {
                    map.put(key, CommenUtil.PFMtoMU(value));
                    map.put(pfmKey, value);
                }
            } else {
                map.put(key, value);
            }
            datas.remove(position);
            datas.add(position, map);
            mMixUIListener.setSelectSave(true);

            if (key.contains("MJ")) {
                mMixUIListener.refreshTBMJ(mjkey);
            }
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
        }

    }


    public interface OnMixListOperateListener {
        public void OnCheckClick(int position, boolean isChecked);

        /**
         * 几何计算按钮点击
         *
         * @param position 位置
         * @param btn      几何计算按钮
         * @param mj_edit  对应的面积输入框
         */
        public void OnBtnClick(int position, Button btn, EditText mj_edit);
    }

    public interface MixUIListener {
        void setSelectSave(boolean isSelectSave);

        void refreshTBMJ(String key);
    }
}
